<?php
/*
 * @ Nguyễn Như Khang
 * nhukhang095@gmail.com
 */    
class BaseDAO{
    var $utils ;
    var $db ;

    function BaseDAO()
    {
        $this->utils = new CommonUtils;
        $this->db = new DBUtils;
    }
        
    function create($table, $fields, $values)
    {
        $conn = $this->db->getConnection();
        if (!$conn){
            return false;
        }
       		
        $query = "INSERT INTO " . $table . " (";
		$queryValues = "";
		$bFirst = true;
		for($i = 0; $i < count($fields); $i++) {
			if (!$bFirst) {
				$query .= ", ";
			}
			$query .= $fields[$i];
			$bFirst = false;
		}
		$bFirst = true;
		for($i = 0; $i < count($values); $i++) {
			if (!$bFirst) {
				$queryValues .= ", ";
			}
			$queryValues .= "'" . $values[$i] . "'";
			$bFirst = false;
		}
		$query .= ") VALUES (" . $queryValues . ")"; 
        $res = $this->db->executeQuery($query);
        if (isset($res) && $res >= 1) {        
        	$res = mysql_insert_id();
        }
        $this->db->closeConnection();
        return $res;
    }
    function del($table, $id, $idFldName = 'id'){
        $conn = $this->db->getConnection();
        if (!$conn){
            return false;
        }
        $query = "DELETE FROM $table WHERE $idFldName = '" . $id . "'";
        $res = $this->db->executeQuery($query);
        $this->db->closeConnection();
        return $res;
    }
    
 	function executeUpdate($table, $query){
        $conn = $this->db->getConnection();
        if (!$conn){
            return false;
        }
        $res = $this->db->executeQuery($query);
        $this->db->closeConnection();
        return $res;
    }

    function update($table, $fields, $values, $idFldName = 'id')  
    {
        $conn = $this->db->getConnection();
        if (!$conn){
            return false;
        }
		$query = "UPDATE $table SET ";
		$bFirst = true;
		for($i = 0; $i < count($fields); $i++) {
			if (!$bFirst) {
				$query .= ", ";
			}
			$query .= $fields[$i] . " = '" . $values[$i] . "'";
			$bFirst = false;
		}
        $query .= " WHERE $idFldName = '". $values[0] . "'";
		
        $res = $this->db->executeQuery($query);
        $this->db->closeConnection();
        return $res;
    }
    function getItem($table, $fields, $id, $idFldName = 'id'){
        $conn = $this->db->getConnection();
        if (!$conn){
            return false;
        }
        $query = "SELECT * " . " FROM " . $table . " WHERE  $idFldName = '$id'";
        $result  = $this->db->executeQuery($query);

        if ($result) {       
	       $row = $this->db->getReader($result) ;   
		   $enquiry = array();
		   foreach($fields as $f) {
				$enquiry[$f] = $row[$f];			
		   }   	   

		}
		$this->db->closeConnection();
		return $enquiry;
    }
    
    
    function getItems($table, $fields, $queryFilter = '', $orderby = '', $begin = -1, $limit = ''){
        $conn = $this->db->getConnection();
        if (!$conn){
            return false;
        }
        $query = "SELECT * " . " FROM " . $table . " WHERE  1 = 1 ";
		if (isset($queryFilter) && $queryFilter != '') {
		 	$query .=  " AND " . $queryFilter;
		}
   		if (isset($orderby) && $orderby != '') {
        	$query .= " $orderby";
		}
		if (isset($begin) && $begin >= 0) {
        	$query .= "  LIMIT $begin, $limit";
		}
        $result  = $this->db->executeQuery($query);
        if ($result) {
        	$returnres = array();
            while ($row = $this->db->getReader($result)) {
               $enquiry = array();
			   foreach($fields as $f) {
			   		$enquiry[$f] = $row[$f];
			   }             
               
               $returnres[] = $enquiry;
            }
            $this->db->closeConnection();
            return $returnres;
        }
        return false;

    }
    
	function getTotalItems($table, $queryFilter){
        $conn = $this->db->getConnection();
        if (!$conn){
            return false;
        }
        $query = "SELECT COUNT(*) as totalItem " . " FROM " . $table;
        if (isset($queryFilter)) {
        	$query .= " WHERE " . $queryFilter;
        } 
        $result  = $this->db->executeQuery($query);
        if ($result) {       
	       $row = $this->db->getReader($result);   
		   return $row['totalItem'];  	   
		}
		$this->db->closeConnection();
		return 0;
    }
}   
?>